//显示用户信息到页面
function showUserPage(pageIndex) {

	var pageSize = 10;
	$.ajax({
		url: "getUserPage", //后台url
		contentType: 'application/json;charset=utf-8',
		type: "get", //类型，POST或者GET,
		dataType: 'json', //数据返回类型，可以是xml、json等
		data: {
			"pageIndex": pageIndex,
			"pageSize": pageSize
		},
		async: false,
		success: function(data) { //成功，回调函数
			var json = eval(data);

			showPageButton(json.pageNumber, json.pageIndex);

			json = json.list;

			console.log(json)

			var str = ""; //需要添加的html文本
			var btn = ""; //需要添加的按钮

			console.log()

			for(var i = 0; i < json.length; i++) {
				if(json[i].sex === ("1")) {
					json[i].sex = "男"
				} else {
					json[i].sex = "女"
				};

				//处理时间戳为时间格式
				if(json[i].createDate) {
					json[i].createDate = getMyDate(json[i].createDate)
				} else {
					json[i].createDate = '';
				}
				//处理未定义数据的显示
				if(json[i].userRealname == null) {
					json[i].userRealname = '';
				}
				if(json[i].age == null) {
					json[i].age = '';
				}
				if(json[i].userPhone == null) {
					json[i].userPhone = '';
				}
				str +=
					'<tr>' +
					'<td><input type="checkbox"></td>' +
					'<td>' + json[i].userId + '</td>' +
					'<td>' +
					'<a href="#">' + json[i].userName + '</a>' +
					'</td>' +
					'<td>' + json[i].userRealname + '</td>' +
					'<td class="am-hide-sm-only">' + json[i].sex + '</td>' +
					'<td class="am-hide-sm-only">' + json[i].age + '</td>' +
					'<td class="am-hide-sm-only">' + json[i].userPhone + '</td>' +
					'<td class="am-hide-sm-only">' + json[i].createDate + '</td>' +
					'<td>' +
					'<div class="am-btn-toolbar">' +
					'<div class="am-btn-group am-btn-group-xs">' +
					'<button class="am-btn am-btn-default am-btn-xs am-text-secondary" data-toggle="modal" data-target="#Model" onclick="showUserById(' + json[i].userId + ')"><span class="am-icon-pencil-square-o"></span>编辑</button>' +
					'<button class="am-btn am-btn-default am-btn-xs am-hide-sm-only"><span class="am-icon-copy"></span> 复制</button>' +
					'<button class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only delUserBtn" onclick="delUserById(' + json[i].userId + ')"><span class="am-icon-trash-o"></span> 删除</button>' +
					'</div>' +
					'</div>' +
					'</td>' +
					'</tr>';
			};
			$("tbody tr").remove(); //清空现有数据
			$("tbody").append(str); //把后台数据显示到页面
		}
	})
}

//给分页按钮绑定点击事件
function bindClick() {
	/*var pageIndex = 1;*/
	$("ul li a").click(function() {
		var buttonValue = $(this).html()
		if(typeof(buttonValue)) {
			console.log(typeof(buttonValue))
		}
		console.log("buttonValue:%s", buttonValue);

		showUserPage(buttonValue);

	});
	$(".ss1").click(function() {
		var currentPage = $(".am-active a").html();
		if(currentPage - 1 > 0) {
			showUserPage(currentPage - 1);
		}
	});
	$(".ss2").click(function() {
		var currentPage = $(".am-active a").html();
		var lastPage = $("ul li a:last").html();
		if(currentPage + 1 <= lastPage) {
			showUserPage(currentPage - (-1));
		}
	});
}
/**
 * am-disabled 上下页翻页无能
 */
/**
 * 显示分页按钮
 * 传入按钮的个数，和选中页的页码
 */
function showPageButton(buttonCount, pageNum) {
	var str = "";
	str +=
		'<li><span class="ss1">«</span></li>';
	for(var i = 1; i < buttonCount + 1; i++) {
		if(i == pageNum) {
			str += '<li class="am-active"><a>' + i + '</a></li>';
		} else {
			str += '<li><a>' + i + '</a></li>';
		}
	}
	str += '<li><span class="ss2">»</span></li>';
	$("#buttonUl li").remove();
	$("#buttonUl").append(str);
	console.log(str);
	bindClick();
}

//编辑用户信息
function showUserById(userId) {
	$.ajax({
		url: "findById", //后台url
		contentType: 'application/json;charset=utf-8',
		type: "get", //类型，POST或者GET
		data: {
			"id": userId
		},
		dataType: 'json', //数据返回类型，可以是xml、json等
		success: function(json) { //成功，回调函数
			//通过用户id查询信息，展示到模态框中
			var userId = json.userId;
			var userName = json.userName;
			var userRealname = json.userRealname;
			var sex = json.sex;
			var age = json.age;
			var userPhone = json.userPhone;
			var createDate = json.createDate;


			$("#userId").val(userId);
			$("#userName").val(userName);
			$("#userRealname").val(userRealname);
			$("#sex").val(sex);
			$("#age").val(age);
			$("#userPhone").val(userPhone);
			if(createDate) {
				$("#createDate").val(getMyDate(createDate));
			} else {
				$("#createDate").val("");
			}

			if(sex == 1) {
				$("#male").attr("checked", "checked")
			} else {
				$("#female").attr("checked", "checked")
			}

		}
	});
}

//删除用户
function delUserById(id) {
	var code = confirm("确定要删除id为" + id + "的用户吗？");
	if(code) {
		$.ajax({
			type: "get",
			url: "userDelete",
			data: {
				"id": id
			},
			dataType: 'text',
			success: function(data) {
				var currentPage = $(".am-active a").html();
				showUserPage(currentPage);
				console.log('currentPage:' + currentPage);
			}
		});
	}

}

//新增按钮时，清空模态框内的值
$("#add").click(function() {
	$(".clean").val("");
	$("input[type=radio]").prop("checked", false);
})

//格式化时间
function getMyDate(str) {
	var oDate = new Date(str),
		oYear = oDate.getFullYear(),
		oMonth = oDate.getMonth() + 1,
		oDay = oDate.getDate(),
		oHour = oDate.getHours(),
		oMin = oDate.getMinutes(),
		oSen = oDate.getSeconds(),
		oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen); //最后拼接时间  
	return oTime;
};
//补0操作
function getzf(num) {
	if(parseInt(num) < 10) {
		num = '0' + num;
	}
	return num;
}

//上一页

//下一页

//页面加载完成后填充数据
$(document).ready(function() {
	showUserPage(1);

})